package BackTrackingPackage2;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Lzm
 * @version 1.0
 */
public class combine_ {
  List<Integer> list = new ArrayList<>();
  List<List<Integer>> result = new ArrayList<>();

  @Test
  public void combineTest(){
    List<List<Integer>> combine = combine(4, 2);
    for (List<Integer> integers : combine) {
      for (Integer integer : integers) {
        System.out.println(integer);
      }
    }
  }
  public List<List<Integer>> combine(int n, int k) {
    backTracking(n,k,1);
    return result;
  }
  public void backTracking(int n,int k,int startIndex){
    // 2. 确定终止条件
    if (list.size() == k){
      result.add(new ArrayList<>(list));
      return;
    }
    for (int i = startIndex ; i <= n ; i++){
      list.add(i);
      backTracking(n,k,i+1);
      list.remove(list.size() - 1);
    }
  }


}
